package com.shanghaionstar.user.service;

import android.content.Context;
import android.os.Bundle;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.request.PrepaidCardRequest;
import com.onstar.cn.more.response.PrepaidCardResponse;
import com.shanghaionstar.enumtype.OSTypeEnum;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.LinkedHashMap;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Mengzhou Gu on 2016/8/2.
 */
public class PayCardDetailService extends BaseService<PrepaidCardResponse> {
    private Context context;
    private Bundle  bundle;

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public PrepaidCardResponse jsonToBean(String json) {
        return null;
    }

    public PayCardDetailService(Context context, Bundle bundle) {
        this.context = context;
        this.bundle = bundle;
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<PrepaidCardResponse> getObserval() {
        return Observable.defer(new Func0<Observable<PrepaidCardResponse>>() {
            @Override
            public Observable<PrepaidCardResponse> call() {
                //
                try {
                    UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity entity = NetConnectUtil.getAuthRequestEntity(context);
                    PrepaidCardRequest request = new PrepaidCardRequest();
                    request.setCardNo(bundle.getString("card_number"));
                    request.setPassWord(bundle.getString("card_password"));
                    request.setOsType(String.valueOf(OSTypeEnum.ANDROID_SMALL));
                    if (bundle != null && bundle.containsKey(FinalConstants.PREPAID_CARD_ACTIVATE_TYPE)) {
                        String activateType = bundle.getString(FinalConstants.PREPAID_CARD_ACTIVATE_TYPE);
                        if (!CommonUtil.isNull(activateType)) {
                            request.setPrepaidCardActivateType(activateType);
                        }
                    }
                    request.setOperation("ACTIVATE");
                    entity.setBody(GsonHelper.toJson(request));
                    LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
                    customHeads.put("Authorization", ApplicationUtil.getInstance().getToken());
                    entity.setCustomHeads(customHeads);
                    String idpId = "";
                    String accountId = ApplicationUtil.getInstance().getAccountId();
                    if (CommonUtil.isNull(idpId)) {
                        idpId = ApplicationUtil.getInstance().getIdpID();
                    }
                    if (CommonUtil.isNull(accountId)) {
                        accountId = "1";
                    }
                    if (CommonUtil.isNull(idpId)) {
                        idpId = "1";
                    }
                    entity = service.activatePrepaidCard(entity, idpId, accountId, bundle.getString("vin")); //激活传参
                    entity =  HttpInjectUtils.injectHttp(PayCardDetailService.this.getClass(), "getObserval", entity);

                    return Observable.just(GsonHelper.fromJson( BusinessUtil.getResponse(getDatas(entity), context),PrepaidCardResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }

            }
        });
    }
}
